package com.base.cn.platform.os.controller.op.keyword;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.op.keyword.OpKeywordService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 敏感词管理
 * @author zsw
 * @since 2018/4/19
 */
@Controller
@RequestMapping("/admin/op/keyword")
public class AdminOpKeywordController extends AdminBaseController {

    @Autowired
    private OpKeywordService opKeywordService;

    /**
     * 查询敏感词列表
     * @param model
     * @return 敏感词列表页面
     */
    @RequestMapping("/queryKeywordList")
    public String queryKeywordList(Model model){
        return resultPage("/op/keyword/keyword-list");
    }

    /**
     * ajax加载敏感词列表
     * @param model
     * @param currentPage 当前页码
     * @param params 查询条件
     * @return 敏感词列表页面
     */
    @RequestMapping("/ajaxQueryKeywordList")
    public String ajaxQueryKeywordList(Model model,
                                        @RequestParam(value = "currentPage", required = false,defaultValue = "1")int currentPage,
                                        @RequestBody Map<String, Object> params){
        Map<String, Object> dataMap = opKeywordService.queryKeywordList(currentPage, params,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/keyword/keyword-ajax-list");
    }

    /**
     *添加敏感词
     * @param request
     * @return
     */
    @RequestMapping("/saveKeyword")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加敏感词")
    public Map<String,Object> saveKeyword(HttpServletRequest request){
        Map<String, Object> keywordType = this.getParamsMap(request);
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        keywordType.put("createUserId", sysUserId);
        return opKeywordService.saveKeyword(keywordType);
    }

    /**
     * 删除敏感词
     * @param keyWordIds 敏感词id串
     * @return
     */
    @RequestMapping("/deleteKeyword")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除敏感词")
    public Map<String,Object> deleteKeyword(@RequestParam("keyWordIds")String keyWordIds){
        return opKeywordService.deleteKeyword(keyWordIds);
    }

    /**
     * 上传敏感词
     * @param request
     * @return
     */
    @RequestMapping("/importKeyWord")
    @ResponseBody
    public Map<String,Object> importKeyWord(HttpServletRequest request,
                                               @RequestParam("file") MultipartFile multipartFile){
        String referer = request.getHeader("referer");
        if(!StringUtils.isNotEmpty(referer)){
            return ResultUtil.ERROR("非法请求，上传失败");
        }
        HSSFWorkbook wookbook = null;
        try {
            wookbook = new HSSFWorkbook(multipartFile.getInputStream());
        } catch (Exception e) {
            return ResultUtil.ERROR("读取xls表格数据错误");
        }
        //只读取sheet1
        HSSFSheet sheet = wookbook.getSheetAt(0);
        int rows = sheet.getPhysicalNumberOfRows(); //excel中实际行数
        if (rows == 0) {
            return ResultUtil.ERROR("xls表格中没有填写数据");
        }
        if (rows > 2000) {
            return ResultUtil.ERROR("xls表中的数据不能多于2000行");
        }
        Map<String,Object> opkeyword;
        HSSFRow row;
        HSSFCell cell;
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        for (int i = 1; i <= rows; i++) {
            opkeyword = new HashMap<>();
            // 读取行为第二行开始
            row = sheet.getRow(i);
            if (row != null) {
                row.getPhysicalNumberOfCells();
                // **读取行中的列**
                cell = row.getCell((short) 0);//敏感词
                String word = null;
                if (cell != null) {
                    word = cell.getStringCellValue().trim();
                    opkeyword.put("word",word);
                    opkeyword.put("createUserId",sysUserId);
                }
                opKeywordService.saveKeyword(opkeyword);
            }
        }
        return ResultUtil.SUCCESS("导入成功");
    }

}